package com.servlets;

import com.alibaba.fastjson.JSON;
import com.pojo.CarouselFigure;
import com.pojo.PageBean;
import com.service.CarouselFigureService;
import com.service.impl.CarouselFigureServiceImpl;
import com.utils.JDBCUtil;
import jdk.nashorn.internal.scripts.JD;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@WebServlet( "/carouselFigure.do")
public class CarouselFigureServlet extends BaseServlet {
    private CarouselFigureService service = new CarouselFigureServiceImpl();

    public void findAll(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String current = request.getParameter("current");
        String limit = request.getParameter("limit");
        Integer curr=1,lim = 10;
        if (current!=null)
            curr=Integer.parseInt(current);
        if (limit!=null)
            lim = Integer.parseInt(limit);
        PageBean<CarouselFigure> page = service.findAll(lim, curr);
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JDBCUtil.toLayuiJson(page.getTotal(),page.getData()));
    }

    public void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        CarouselFigure cf = new CarouselFigure();
        response.setContentType("application/json;charset=utf-8");
        String url = request.getParameter("url"),msg;
        if (url!=null){
            cf.setUrl(url);
            cf.setId(UUID.randomUUID().toString().replace("-","").substring(0,10));
            if (service.addCF(cf))
                msg="成功";
            else
                msg="失败";
        }else
            msg = "图片路径为空";
        response.getWriter().write(JSON.toJSONString(msg));
    }

    public void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id"),msg;
        if(service.deleteCF(id))
            msg="删除成功";
        else
            msg="删除失败";
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(msg));
    }

    public void findByID(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        String id = request.getParameter("id");
        response.setContentType("application/json;charset=utf-8");
        CarouselFigure cf = service.findByID(id);
        int maxNum = service.findMaxSerialNumber();
        response.getWriter().write(JSON.toJSONString(cf).replace("}",",\"maxNum\":\""+maxNum+"\"}"));
    }

    public void update(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        Map<String, String[]> map = request.getParameterMap();
        CarouselFigure cf = JDBCUtil.mapToBean(map, CarouselFigure.class);
        System.out.println(cf);
        String msg = "";
        if(service.updateCF(cf))
            msg="成功";
        else
            msg="失败";
        response.setContentType("application/json;charset=utf-8");
        response.getWriter().write(JSON.toJSONString(msg));
    }
}
